let wins = null;//所有赢法
let count = 0;//赢法统计
function getWins(chessArr) {
    if(!wins){
        let arr = [];
        for (let i = 0; i < 15; i++) {
            arr[i] = [];
            for (let j = 0; j < 15; j++) {
                arr[i][j] = {};
            }
        }
        count = 0;
        //横线
        for (let i = 0; i < 15; i++) {
            for (let j = 0; j < 11; j++) {
                for (let k = 0; k < 5; k++) {
                    arr[i][j+k][count] = true;
                }
                count++;
            }
        }
        //竖线
        for (let i = 0; i < 15; i++) {
            for (let j = 0; j < 11; j++) {
                for (let k = 0; k < 5; k++) {
                    arr[j+k][i][count] = true;
                }
                count++;
            }
        }
        //正斜线
        for (let i = 0; i < 11; i++) {
            for (let j = 0; j < 11; j++) {
                for (let k = 0; k < 5; k++) {
                    arr[i+k][j+k][count] = true;
                }
                count++;
            }
        }
        //反斜线
        for (let i = 0; i < 11; i++) {
            for (let j = 14; j > 3; j--) {
                for (let k = 0; k < 5; k++) {
                    arr[i+k][j-k][count] = true;
                }
                count++;
            }
        }
        wins = arr;
    }
    let p1 = Array(count).fill(0),p2 = Array(count).fill(0);
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 15; j++) {
            if(chessArr[i][j]==1){
                for (const key in wins[i][j]) {
                    p1[key] += 1;
                    p2[key] = 6;
                }
            }else if(chessArr[i][j]==2){
                for (const key in wins[i][j]) {
                    p2[key] += 1;
                    p1[key] = 6;
                }
            }
        }
    }
    return {p1,p2};
}

let enArr = [1,200,500,2000,10000],myArr = [1,220,440,2200,20000];
module.exports= function AIFunc(key,chessArr) {
    let enKey = key==1?2:1;//敌方
    let max = -1;//最大权值
    let m = 0,n = 0;//最优位置
    let enS=[],myS=[];//每个位置的权值
    let obj = getWins(chessArr);
    let enP = obj[`p${enKey}`],myP = obj[`p${key}`];
    for (let i = 0; i < 15; i++) {
        enS[i] = []; myS[i] = [];
        for (let j = 0; j < 15; j++) {
            enS[i][j] = 0; myS[i][j] = 0;
            if(chessArr[i][j]==0){
                for (const key in wins[i][j]) {
                    if(enP[key]<5){
                        enS[i][j] += enArr[enP[key]];
                    }
                    if(myP[key]<5){
                        myS[i][j] += myArr[myP[key]];
                    }
                }
                if(enS[i][j]>max){
                    max = enS[i][j];
                    m = i; n = j;
                }else if(enS[i][j]==max&&myS[i][j]>myS[m][n]){//我方在此落子更有利
                    //敌方此点权值等于最大权值,判断我方在此点权值与最大权值点的大小,取大值点
                    m = i; n = j;
                }
                if(myS[i][j]>max){
                    max = myS[i][j];
                    m = i; n = j;
                }else if(myS[i][j]==max&&enS[i][j]>enS[m][n]){//我方在此落子,对敌方更不利
                    //我方此点权值等于最大权值,判断敌方在此点权值与最大权值点的大小,取大值点
                    m = i; n = j;
                }
            }
        }
    }
    return [m,n];
}